using System;
using System.Collections.Generic;
using System.Text;
using InfoExpress.ESM.Services;
using InfoExpress.ESM.Domains;

namespace InfoExpress.ESM.WinApp
{
  public static class GlobalCache
  {
    #region AccountService
    private static AccountService _accountService = null;

    /// <summary>
    /// Account service.
    /// </summary>
    public static AccountService AccountService
    {
      get
      {
        if (_accountService == null)
          _accountService = new AccountService();

        return _accountService;
      }
    }
    #endregion

    #region StudentService
    private static StudentService _studentService = null;

    /// <summary>
    /// Student service.
    /// </summary>
    public static StudentService StudentService
    {
      get
      {
        if (_studentService == null)
          _studentService = new StudentService();

        return _studentService;
      }
    }
    #endregion

    #region Class Service
    private static SafeNameClassService _classService = null;

    /// <summary>
    /// Class service.
    /// </summary>
    public static SafeNameClassService ClassService
    {
      get
      {
        if (_classService == null)
          _classService = new SafeNameClassService();

        return _classService;
      }
    }
    #endregion

    #region Class Schedule Service
    private static ClassScheduleService _classScheduleService = null;

    /// <summary>
    /// Class schedule service.
    /// </summary>
    public static ClassScheduleService ClassScheduleService
    {
      get
      {
        if (_classScheduleService == null)
          _classScheduleService = new ClassScheduleService();

        return _classScheduleService;
      }
    }
    #endregion

    #region Class Teacher Schedule Service
    private static ClassTeacherScheduleService _classTeacherScheduleService = null;

    /// <summary>
    /// Class teacher schedule service.
    /// </summary>
    public static ClassTeacherScheduleService ClassTeacherScheduleService
    {
      get
      {
        if (_classTeacherScheduleService == null)
          _classTeacherScheduleService = new ClassTeacherScheduleService();

        return _classTeacherScheduleService;
      }
    }
    #endregion

    #region Teacher Service
    private static TeacherService _teacerService = null;

    /// <summary>
    /// Student service.
    /// </summary>
    public static TeacherService TeacherService
    {
      get
      {
        if (_teacerService == null)
          _teacerService = new TeacherService();

        return _teacerService;
      }
    }
    #endregion

    #region EnumsService
    public static EnumsService _enumsService = null;

    /// <summary>
    /// Enums service
    /// </summary>
    public static EnumsService EnumsService
    {
      get
      {
        if (_enumsService == null)
          _enumsService = new EnumsService();

        return _enumsService;
      }
    }
    #endregion

    #region PeriodService
    private static PeriodService _periodService = null;
    /// <summary>
    /// Period Service
    /// </summary>
    public static PeriodService PeriodService
    {
      get
      {
        if (_periodService == null)
          _periodService = new PeriodService();

        return _periodService;
      }
    }

    #endregion

    #region SessionService
    private static SessionService _sessionService = null;
    /// <summary>
    /// Session Service
    /// </summary>
    public static SessionService SessionService
    {
      get
      {
        if (_sessionService == null)
          _sessionService = new SessionService();

        return _sessionService;
      }
    }
    #endregion

    #region Session Period Service
    private static SessionPeriodService _sessionPeriodervice = null;
    /// <summary>
    /// Session Period Service
    /// </summary>
    public static SessionPeriodService SessionPeriodService
    {
      get
      {
        if (_sessionPeriodervice == null)
          _sessionPeriodervice = new SessionPeriodService();

        return _sessionPeriodervice;
      }
    }

    #endregion

    #region Program Service
    private static ProgramService _programService = null;
    /// <summary>
    /// Session Service
    /// </summary>
    public static ProgramService ProgramService
    {
      get
      {
        if (_programService == null)
          _programService = new ProgramService();

        return _programService;
      }
    }
    #endregion

    #region Shift Service
    private static ShiftService _shiftService = null;
    /// <summary>
    /// Shift Service
    /// </summary>
    public static ShiftService ShiftService
    {
      get
      {
        if (_shiftService == null)
          _shiftService = new ShiftService();

        return _shiftService;
      }
    }
    #endregion

    #region Shift Period Service
    private static PeriodShiftService _shiftPeriodService = null;
    /// <summary>
    /// Shift Service
    /// </summary>
    public static PeriodShiftService ShiftPeriodService
    {
      get
      {
        if (_shiftPeriodService == null)
          _shiftPeriodService = new PeriodShiftService();

        return _shiftPeriodService;
      }
    }
    #endregion

    #region Program Shift Service
    private static ProgramShiftService _programShiftService = null;
    /// <summary>
    /// Session Service
    /// </summary>
    public static ProgramShiftService ProgramShiftService
    {
      get
      {
        if (_programShiftService == null)
          _programShiftService = new ProgramShiftService();

        return _programShiftService;
      }
    }
    #endregion

    #region Detail Period Service
    private static DetailPeriodService _detailPeriodService = null;
    /// <summary>
    /// Detail Period Service
    /// </summary>
    public static DetailPeriodService DetailPeriodService
    {
      get
      {
        if (_detailPeriodService == null)
          _detailPeriodService = new DetailPeriodService();

        return _detailPeriodService;
      }
    }
    #endregion

    #region Free Period Service
    private static FreePeriodService _freePeriodService = null;
    /// <summary>
    /// Detail Period Service
    /// </summary>
    public static FreePeriodService FreePeriodService
    {
      get
      {
        if (_freePeriodService == null)
          _freePeriodService = new FreePeriodService();

        return _freePeriodService;
      }
    }
    #endregion

    #region Detail Miss Period Service
    private static DetailMissPeriodService _detailMissPeriodService = null;
    /// <summary>
    /// Detail Miss Period Service
    /// </summary>
    public static DetailMissPeriodService DetailMissPeriodService
    {
      get
      {
        if (_detailMissPeriodService == null)
          _detailMissPeriodService = new DetailMissPeriodService();

        return _detailMissPeriodService;
      }
    }
    #endregion

    #region CurrentUser
    private static Account _currentUser = null;
    /// <summary>
    /// Authenticated user.
    /// </summary>
    public static Account CurrentUser
    {
      get { return _currentUser; }
      set { _currentUser = value; }
    }
    #endregion

    #region Period List/Map
    /// <summary>
    /// Max periods.
    /// </summary>
    private static readonly int NumberOfPeriod = 7 * 3 * 6;
    private static TList<Period> _periodList = null;
    /// <summary>
    /// Period List
    /// </summary>
    public static TList<Period> PeriodList
    {
      get
      {
        if (_periodList == null)
        {
          _periodList = GlobalCache.PeriodService.GetAll();
          _periodList.Sort("Code ASC");
          if (_periodList.Count != NumberOfPeriod)
            throw new Exception("There is not enough period in database.");
        }

        return _periodList;
      }
    }

    public static Dictionary<string, Period> _periodNameMap = null;
    /// <summary>
    /// Period Name map.
    /// </summary>
    public static Dictionary<string, Period> PeriodNameMap
    {
      get
      {
        if (_periodNameMap == null)
        {
          _periodNameMap = new Dictionary<string, Period>();
          foreach (Period period in PeriodList)
          {
            _periodNameMap.Add(period.Code, period);
          }
        }

        return _periodNameMap;
      }
    }

    public static Dictionary<int, Period> _periodIdMap = null;
    /// <summary>
    /// Period Id map.
    /// </summary>
    public static Dictionary<int, Period> PeriodIdMap
    {
      get
      {
        if (_periodIdMap == null)
        {
          _periodIdMap = new Dictionary<int, Period>();
          foreach (Period period in PeriodList)
          {
            _periodIdMap.Add(period.Id, period);
          }
        }

        return _periodIdMap;
      }
    }

    #endregion

    #region Class List/Map

    private static TList<SafeNameClass> _classList = null;
    /// <summary>
    /// Period List
    /// </summary>
    public static TList<SafeNameClass> ClassList
    {
      get
      {
        if (_classList == null)
        {
          _classList = GlobalCache.ClassService.GetAll();
          _classList.Sort("Code ASC");
        }

        return _classList;
      }
    }

    public static Dictionary<string, SafeNameClass> _classNameMap = null;
    /// <summary>
    /// Class Name map.
    /// </summary>
    public static Dictionary<string, SafeNameClass> ClassNameMap
    {
      get
      {
        if (_classNameMap == null)
        {
          _classNameMap = new Dictionary<string, SafeNameClass>();
          foreach (SafeNameClass cl in ClassList)
          {
            _classNameMap.Add(cl.Code, cl);
          }
        }

        return _classNameMap;
      }
    }

    public static Dictionary<int, SafeNameClass> _classIdMap = null;
    /// <summary>
    /// Class Id map.
    /// </summary>
    public static Dictionary<int, SafeNameClass> ClassIdMap
    {
      get
      {
        if (_classIdMap == null)
        {
          _classIdMap = new Dictionary<int, SafeNameClass>();
          foreach (SafeNameClass cl in ClassList)
          {
            _classIdMap.Add(cl.Id, cl);
          }
        }

        return _classIdMap;
      }
    }

    /// <summary>
    /// Refresh globl class map/list 
    /// </summary>
    public static void RefreshClassListMap()
    {
      _classList = null;
      _classNameMap = null;
      _classIdMap = null;
    }

    #endregion
  }
}
